package com.lanou.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.Consultant;
import com.lanou.entity.Course;
import com.lanou.entity.Tag;
import com.lanou.mapper.ConsultantLevelMapper;
import com.lanou.mapper.ConsultantMapper;
import com.lanou.mapper.TagMapper;
import com.lanou.service.ConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ConsultantServiceImpl
 * @Description TODO
 * @Author chenzhaobao
 * @Date 2025年08月13日 11:24
 * @Version 1.0
 */
@Service
public class ConsultantServiceImpl implements ConsultantService {
    @Autowired
    private ConsultantMapper consultantMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ConsultantLevelMapper consultantLevelMapper;

    @Override
    public int insertConsultant(Consultant consultant) {
        return consultantMapper.insertConsultant(consultant);
    }

    @Override
    public int deleteConsultantById(Integer id) {
        return consultantMapper.deleteConsultantById(id);
    }

    @Override
    public int deleteConsultantsByIds(List<Integer> ids) {
        return consultantMapper.deleteConsultantByIds(ids);
    }

    @Override
    public int updateConsultant(Consultant consultant) {
        return consultantMapper.updateConsultant(consultant);
    }

    @Override
    public Consultant selectConsultantById(Integer id) {
        return consultantMapper.selectConsultantById(id);
    }


    @Override
    public PageInfo<Consultant> selectConsultantByPage(Consultant consultant, int pageNum, int pageSize) {
        // 1. 开启分页（PageHelper 会自动拦截后续 SQL 并添加分页逻辑）
        PageHelper.startPage(pageNum, pageSize);

        // 2. 执行查询（此时返回的 list 已被 PageHelper 处理为分页对象）
        List<Consultant> consultants = consultantMapper.selectConsultantByPage(consultant);

        // 3. 封装分页信息（自动包含总条数、总页数等）
        return new PageInfo<>(consultants);
    }

    // 根据等级ID查询咨询师
    @Override
    public List<Consultant> selectConsultantsByLevelId(Integer levelId) {
        // 1. 调用Mapper查询该等级下的咨询师（含等级信息）
        List<Consultant> consultants = consultantMapper.selectConsultantsByLevelId(levelId);

        // 2. 补充每个咨询师的标签信息（可选，根据需求决定）
        for (Consultant consultant : consultants) {
            List<Tag> tags = tagMapper.selectTagsByConsultantId(consultant.getId());
            consultant.setTags(tags);  // 存入咨询师对象的tags属性
        }

        return consultants;
    }

    // 根据标签ID查询咨询师
    @Override
    public List<Consultant> selectConsultantsByTagId(Integer tagId) {
        // 1. 调用Mapper查询该标签关联的咨询师（含等级信息）
        List<Consultant> consultants = consultantMapper.selectConsultantsByTagId(tagId);

        // 2. 补充每个咨询师的标签信息（可选）
        for (Consultant consultant : consultants) {
            List<Tag> tags = tagMapper.selectTagsByConsultantId(consultant.getId());
            consultant.setTags(tags);
        }

        return consultants;
    }

    @Override
    public List<Course> selectCoursesByConsultantId(Integer consultantId) {
        return consultantMapper.selectCoursesByConsultantId(consultantId);
    }

    @Override
    public List<Consultant> selectConsultantsByFilter(
            String region,
            List<Integer> tagIds,
            String levelName,
            String gender

    ) {
        return consultantMapper.selectConsultantsByFilter(region, tagIds, levelName, gender);
    }

    //根据咨询师等级和咨询类别级联查询
    @Override
    public List<Consultant> selectConsultantByLevelAndSort(String levelName, String sort) {
        List<Consultant> consultants = consultantMapper.selectConsultantByLevelAndSort(levelName, sort);
        return consultants;
    }
}
